package com.mycompany.services.dao;

import java.util.List;

import org.hibernate.Session;
import org.hibernate.SessionFactory;

import com.mycompany.optimizaproject.Incidente;
import com.mycompany.optimizaproject.Solucion;
import com.mycompany.optimizaproject.Usuario;

public class UsuarioDAO extends AbstractHibernateDAO<Usuario, Integer> {

	public UsuarioDAO(SessionFactory sessionFactory){
		setSessionFactory(sessionFactory);
	}
	@Override
	protected Class<Usuario> getDomainClass() {
		return Usuario.class;
	}
	
	@SuppressWarnings("unchecked")
	public List<Usuario> loginUser(String user, String password){
		return getHibernateTemplate().find("from Usuario where usuario = '" + user + "' and password= '" + password +"'");
	}
	
	
	   public int nuevoUsuario(Usuario user){
		   	SessionFactory sessionFactory=this.getSessionFactory();
	        Session session=sessionFactory.openSession();
	        org.hibernate.Transaction transaction= session.beginTransaction();
	        Usuario u = user;
	       
	        session.saveOrUpdate(u);
	        
	        try {
				transaction.commit();
			} catch (SecurityException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				return 0;
			} catch (IllegalStateException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				return 0;
			}
	        session.close();
	        System.out.println("======= User Inserted ========");
	        return 1;
	   }

}
